<?php

namespace App\Providers;


use Aitels\Foundation\Annotation\ApplicationAnnotationsContainer;
use Aitels\Foundation\Annotation\ConfigAnnotation;
use Aitels\Foundation\Annotation\ListeningAnnotation;
use Aitels\Foundation\Annotation\RouteAnnotation;
use Aitels\Foundation\Annotation\ScheduleAnnotation;
use Aitels\Foundation\Config\AnnotationConfigRegister;
use Aitels\Foundation\Event\AnnotationListenerRegister;
use Aitels\Foundation\Scanner\Scanner;
use Illuminate\Support\ServiceProvider;

class AnnotationServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if (env('ANNOTATION_SCAN')) {
            //实例化扫描器，扫描目录
            $scanner = new Scanner([
                base_path('modules/web'),
                base_path('modules/admin'),
                base_path('modules/h5'),
                base_path('modules/common')
            ]);

            ApplicationAnnotationsContainer::setCachePath(storage_path('annotation/cache.php'));
            //实例化一个应用注解容器
            $annotationContainer = new ApplicationAnnotationsContainer(
                ListeningAnnotation::class,//事件注解
                RouteAnnotation::class,//路由注解
                ScheduleAnnotation::class,//定时任务注解
                ConfigAnnotation::class//config注解
            );
            //注册文件扫描回调函数
            $scanner->registerHandler(function (\SplFileInfo $fileInfo) use ($annotationContainer) {
                $annotationContainer->handle($fileInfo);
            });
            //开始扫描
            $scanner->scan();
            //每次扫描后缓存扫描结果，关闭ANNOTATION_SCAN后直接读缓存
            ApplicationAnnotationsContainer::cacheAnnotation();
        } else {
            ApplicationAnnotationsContainer::setCachePath(storage_path('annotation/cache.php'));
            ApplicationAnnotationsContainer::loadCache();
        }

        //路由注解注册
        (new \Aitels\Foundation\Route\AnnotationRouteRegister())->register();

        //事件注解注册
        (new AnnotationListenerRegister())->register();

        //config 注解注册
        (new AnnotationConfigRegister())->register($this->app);

        //定时任务注解注册在App\Console\Kernel中...

        //更多注解...
    }

}
